#!/bin/bash -ex

WWW_DIR=/data/www
APP_DIR=$WWW_DIR/pawhub-pos

deploy() {
	ip="$1"

	cd "$WORKSPACE"

	# Sync code
	sudo rsync -ruv --exclude='.git' ./ $ip:$APP_DIR/

	# Change permission
	sudo ssh $ip "chmod -R 777 $APP_DIR/storage/"
	sudo ssh $ip "chmod -R 777 $APP_DIR/bootstrap/cache/"
}

server1=124.221.14.235
server2=124.220.103.16

cd "$WORKSPACE"
sudo scp $server1:$WWW_DIR/pawhub-pos-env/env .env

deploy $server1
deploy $server2

# Restart supervisord
sudo ssh $server1 "kubectl delete -f /data/pawhub-pos/deployment/supervisor.yaml"
sudo ssh $server1 "kubectl apply -f /data/pawhub-pos/deployment/supervisor.yaml"
